package com.ktjiaoyu.t351.controller;

import com.ktjiaoyu.t351.entity.SysUser;
import com.ktjiaoyu.t351.exception.LoginException;
import com.ktjiaoyu.t351.service.user.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName : LoginController
 * @Description :
 * @Author : Lzz
 * @Date: 2023/10/16 09:57:48
 */
@Controller
public class LoginController {

    @Resource
    private SysUserService sysUserService;

    /**
     * 跳转到login页面
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(){

        return "login";
    }

    /**
     * 去到主页
     * @return
     */
    @RequestMapping("/toMain")
    public String toMain(){

        return "frame";
    }

    /**
     * 处理登录请求
     * @param sysUser
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/login")
    public String login(SysUser sysUser, HttpServletRequest request, HttpServletResponse response) throws LoginException {
        SysUser user = sysUserService.login(sysUser);
        HttpSession session = request.getSession();
        session.setAttribute("userSession", user);
        return "redirect:/toMain";
    }

    /*@ExceptionHandler(value = {Exception.class})
    public String loginException(LoginException e,HttpServletRequest request){
        request.setAttribute("error", e);
        return "login";
    }*/

    /*@ExceptionHandler(value = {RuntimeException.class})
    public String handlerException(RuntimeException e,HttpServletRequest req){
        req.setAttribute("error",e);
        return "sysError";
    }*/



}
